package com.itheima.controller;/*
@Author:李正铠
@Date:2020年04月16日16时44分
*/

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RolerManageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/roleManage")
public class RoleManageController {

    @Reference
    private RolerManageService rolerManageService;


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = rolerManageService.findPage(queryPageBean);
            return new Result(true, MessageConstant.GET_ROLEMANAGE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLEMANAGE_FAIL);
        }
    }




    /**
     * 编辑回显 根据id查询对应的角色数据
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/findById")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result findById(Integer roleId){
        Role role= null;
        try {
            role = rolerManageService.findById(roleId);
        return new Result(true,MessageConstant.GET_ROLEMANAGE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLEMANAGE_FAIL);
        }
    }

    /**
     *编辑回显 查询role与permission关联的数据
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/findRoleAndPermissionByRoleId")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public  Result findRoleAndPermissionByRoleId(Integer roleId){
        try {
            List<Integer>permissionIds= rolerManageService.findRoleAndPermissionByRoleId(roleId);
            return new Result(true,MessageConstant.GET_AUTHORITYMANAGE_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_AUTHORITYMANAGE_FAIL);
        }
    }



    /**
     * 编辑角色数据
     * @param param 角色信息
     */
    @RequestMapping(value = "/update")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result update(@RequestBody HashMap<String,Object> param){
        try {
            rolerManageService.update(param);
            return new Result(true,MessageConstant.EDIT_ROLEMANAGE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_UTHORITYMANAGE_FAIL);
        }
    }

    /**
     * 添加角色数据
     * @param
     * @return
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody HashMap<String,Object> map){
        try {
            rolerManageService.add(map);
            return new Result(true,MessageConstant.ADD_ROLEMANAGE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLEMANAGE_FAIL);
        }
    }

    /**
     * 删除角色项
     * @return
     */
    @RequestMapping(value = "/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result deleteRole(Integer roleId){
        try {
            rolerManageService.deleteRole(roleId);
            return new Result(true,MessageConstant.DELETE_ROLEMANAGE_SUCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLEMANAGE_FAIL);
        }
    }

    /**
     * 查询所有角色
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('USER_ADD','USER_EDIT')")
    public Result findAll() {
        try {
            List<Role> roleList =  rolerManageService.findAll();
            return  new Result(true,MessageConstant.GET_ROLEMANAGE_lIST_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(true,MessageConstant.GET_ROLEMANAGE_lIST_FAIL);
        }
    }


}
